<?php

class InvSeleksiAkhirModel{
	private $db;
	private $subbagian;
	
	private $validColumn;

	private $data;

	private $result;

	private $sort_by;
	private $sort_order;

	private $search_keyword;
	private $search_field;

	private $limit;
	
	function InvSeleksiAkhirModel($dbObject){
		$this->subbagian 	= 'inv_seleksi_akhir';		
		$this->db 			= $dbObject;

		$this->validColumn  = array(
			'tgl_masuk', 
			'label_tandan', 
			'jml_biji_terima',
			'jml_biji_discard', 
			'jml_biji_seleksi',
			'jml_biji_saleable',
			'jml_biji_nursery',
            'penanggung_jawab'
			);
		
		// settting up configuration
		if(($this->start=$this->getGET('start')) == NULL)
			$this->start = 0;

		$this->sort_by 			= $this->getGET('sort_by');
		$this->sort_order 		= $this->getGET('sort_order');
		$this->search_keyword	= $this->getGET('search_keyword');
		$this->search_field		= $this->getGET('search_field');

		if(($this->limit=$this->getGET('limit')) == NULL)
			$this->limit = 50;		
	}

	private function getGET($param){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return NULL;
	}
	
	public function countAll(){
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE ".$this->search_field."='".$this->search_keyword."'";
			
		$q = "SELECT COUNT(*) FROM `caramel_operational`.`inv_seleksi_akhir` $where";
		
		$this->total_record = $this->db->get_var($q);
		
		return $this->total_record;
	}	
	
	public function search(){			
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE ".$this->search_field."='".$this->search_keyword."'";

		// get data
		if( $this->sort_by )
			$q = "
			SELECT 
				`id`, `tgl_masuk`, `label_tandan`, `jml_biji_terima`, `jml_biji_discard`, `jml_biji_seleksi`, `jml_biji_saleable`, `jml_biji_nursery`, `penanggung_jawab`
			FROM `caramel_operational`.`inv_seleksi_akhir` $where
			ORDER BY ".$this->sort_by." ".$this->sort_order." LIMIT ".$this->start.",".$this->limit."";
		else
			$q = "
			SELECT 
				`id`, `tgl_masuk`, `label_tandan`, `jml_biji_terima`, `jml_biji_discard`, `jml_biji_seleksi`, `jml_biji_saleable`, `jml_biji_nursery`, `penanggung_jawab`
			FROM `caramel_operational`.`inv_seleksi_akhir` $where
			ORDER BY `tgl_masuk` LIMIT ".$this->start.",".$this->limit."";

		$this->result = $this->db->get_results($q, ARRAY_A);
		return $this->result;
	}

	public function getResult(){
		return $this->result;
	}
	
	
	function format($data){
		$result = array();
		foreach($this->validColumn as $v){
			if( isset($data[$v]) )
				$result[$v] = $data[$v];
			else
				$result[$v] = NULL;
		}
		
		return $result;
	}
	
	private function insertInvSeleksiAkhir(){
		$dataInvSeleksiAkhir   = array();
		$invSeleksiAkhirColumn = array(
			'tgl_masuk', 
			'label_tandan', 
			'jml_biji_terima',
			'jml_biji_discard', 
			'jml_biji_seleksi',
			'jml_biji_saleable',
			'jml_biji_nursery',
            'penanggung_jawab');
		
		// copy dari $data ke $dataInvSeleksiAkhir
		foreach($invSeleksiAkhirColumn as $c){
			$dataInvSeleksiAkhir[$c] = $this->data[$c];
		}
		
		$qHeader = "(`".implode($invSeleksiAkhirColumn, "`, `")."`)";
		$qBody   = "('".implode($dataInvSeleksiAkhir, "', '")."')";
		
		$query = "INSERT INTO `caramel_operational`.`inv_seleksi_akhir` ".$qHeader." VALUES ".$qBody;
		$this->db->query($query);
	}	
	
	function put($data){		
		$this->data	= $this->format($data);// buang field2 yang tidak dibutuhkan
		
		if( $this->isExist() == FALSE ){				
			// inventaris
			$this->insertInvSeleksiAkhir();					
		}
		else{
			return INSERT_FAIL_DUPLICATE;
		}		
	}
	
	function isExist(){
		$query = "SELECT COUNT(*) FROM `caramel_operational`.`inv_seleksi_akhir` WHERE `label_tandan`='".$this->data['label_tandan']."'";
		$count = $this->db->get_var($query);
		
		if( $count > 0 )
			return TRUE;
			
		return FALSE; // tidak ada data yang dicari... no duplicate		
	}
    
    public function delete($id){
        $q[] = "
        DELETE FROM `caramel_operational`.`inv_seleksi_akhir` 
        WHERE `id`='$id'";
        
        foreach($q as $query){
            $this->db->query($query);
        }
    }
    
    public function getById($id){
        $q = "SELECT * FROM `caramel_operational`.`inv_seleksi_akhir` WHERE `id`='$id'";
        $r = $this->db->get_results($q, ARRAY_A);
        
        if( is_array($r) && count($r) > 0 ){
            $data = array_pop($r);
            
            return $data;
        }
        else{
            return NULL;
        }
    }
    
    public function editById($data, $id){
        $valid = $this->getById($id);
        if( $valid == NULL ){
            return EDIT_ID_INVALID;
        }
        
        // populate data
        $this->data	= $this->format($data);
        
        $q = "
            UPDATE `caramel_operational`.`inv_seleksi_akhir`
            SET 
                `tgl_masuk`='".$this->data['tgl_masuk']."',
                `label_tandan`='".$this->data['label_tandan']."',
                `jml_biji_terima`='".$this->data['jml_biji_terima']."',
                `jml_biji_discard`='".$this->data['jml_biji_discard']."',
                `jml_biji_seleksi`='".$this->data['jml_biji_seleksi']."',
                `jml_biji_saleable`='".$this->data['jml_biji_saleable']."',
                `jml_biji_nursery`='".$this->data['jml_biji_nursery']."',
                `penanggung_jawab`='".$this->data['penanggung_jawab']."'
            WHERE `id`='$id'
            ";
        if( $this->db->query($q) ){            
            return EDIT_OK;
        }
        else{
            return EDIT_FAIL;
        }
    }
}
?>